/**
 * 1、为便于以后转换使用ReactNative，所有本地接口在此转换，除了网络请求已经在Connection封装
 * 2、如果是部署于web并且有网络API也方便切换
 * cfg 格式
 * var cfg={
 * 		params:{xxx:"xxx"},
 * 		success:function(rs){
 * 		
 * 		},
 * 		error:function(rs){
 * 		
 * 		}
 * }
 */
var cordova=window.cordova?window.cordova:{exec:function(scb,fcb){
	if(fcb){fcb()};
}};
var ServiceApi={ 
	nativeBackEvented:false,//原生返回键的监听状态
	_process:function(cfg,plugin,request){
		cfg=cfg?cfg:{params:{}};
		if(cfg&&cfg.params){
			cfg._params=[JSON.stringify(cfg.params)];
		}else{
			cfg._params=[];
		}
		cordova.exec(cfg.success, cfg.error, plugin, request, cfg._params);
	},
	
	//请求定位权限
	getLocationServiceState:function(cfg){ 
		this._process(cfg, "LocationPlugin", "getLocationServiceState");
	},
	//获取用户定位信息
	getLocationGeoInfo:function(cfg){
		this._process(cfg, "LocationPlugin", "getLocationGeoInfo");
	},
	//语音信息订阅接口
	onVoiceSubscrie:function(cfg){
		this._process(cfg, "SpeechRecognizerPlugin", "onSubscrie");
	},
	//开始监听话筒
	startVoiceListen:function(cfg){
		this._process(cfg, "SpeechRecognizerPlugin", "startListen");
	},
	//停止监听话筒
	stopVoiceListen:function(cfg){
		this._process(cfg, "SpeechRecognizerPlugin", "stopListen");
	},
	//微信支付接口
	payByWX:function(cfg){
		this._process(cfg, "PayPlugin", "isWXInstalled");
	},
	//第三方支付接口
	payByTP:function(cfg){
		this._process(cfg, "PayPlugin", "payOrder");
	},
	//获取设备版本信息
	getSysInfo:function(cfg){
		this._process(cfg, "DevicePlugin", "showInfo");
	},
	//隐藏键盘
	hideKeyboard:function(cfg){
		this._process(cfg, "DevicePlugin", "keyboardDismiss");
	},
	//缓存插件
	cache:{
		get:function(cfg){
			ServiceApi._process(cfg, "KeyValueStorePlugin", "fetchValue");
		},
		set:function(key,value){
			var cfg={
				params:{key:key,value:value},
				success:function(){},
				error:function(){}
			};
			ServiceApi._process(cfg, "KeyValueStorePlugin", "setValue");
		},
		remove:function(key){
			var cfg={
				params:{key:key},
				success:function(){},
				error:function(){}
			};
			ServiceApi._process(cfg, "KeyValueStorePlugin", "removeKey");
		},
		getAllKeys:function(){
			ServiceApi._process(null, "KeyValueStorePlugin", "getAllKey");
		},
		clear:function(){
			ServiceApi._process(null, "KeyValueStorePlugin", "cleanCache");
		},
		getAll:function(callback){
			var cfg={
				params:{},
				success:callback,
				error:callback
			};
			ServiceApi._process(cfg, "KeyValueStorePlugin", "getAll");
		}
	},
	//截图插件
	screenshot:function(cfg){
		this._process(cfg,"DevicePlugin","screenshot");
	},
	//页面跳转插件
	goPage:function(cfg){
		this._process(cfg,"NativeViewPlugin","goPage");
	},
	//顶部title控件
	updateHeader:function(position,cfg){ 
		var maps={
				"left":"setLeftTitleLayout",
				"center":"setMiddleTitleLayout",
				"right":"setRightTitleLayout"
			};
		this._process({
			success:null,
			error:null,
			params:cfg
		},"NativeTitleBarPlugin",maps[position]);
	},
	//H5根页面调用原生返回处理
	nativeBack:function(){
		this._process(null, "H5EventPlugin", "onRootPageBackPressed"); 
	},
	//H5页面渲染完成后，通知native
	afterrender:function(path){ 
		this._process({success:null,error:null,params:{url:path}}, "H5EventPlugin", "onPageLoadFinish");
	},
	//监听原生回退键(android)
	addNativeBackListener:function(cfg){
		cfg.params={"action":"register","status":"onBackPressed"}; 
		this._process(cfg,"NativeEventPlugin","listenEvent");
	},
	//移除监听原生回退键(android)
	removeNativeBackListener:function(){
		var cfg={};
		cfg.params={"action":"unregister","status":"onBackPressed"}; 
		this._process(cfg,"NativeEventPlugin","listenEvent");
	},
	//调用通讯录
	callAddressBook:function(cfg){
		this._process(cfg,"DevicePlugin","callAddressBook");
	},

	//调用网络名称
	currentNetwork:function(cfg){
		this._process(cfg,"DevicePlugin","currentNetwork");
	},

	//立即更新版本插件
	updateVersionNow:function(cfg){
		this._process(cfg,"DevicePlugin","updateVersionNow");
	},

	//判断是否有升级的插件
	isNeedUpdate:function(cfg){
		this._process(cfg,"DevicePlugin","isNeedUpdate");
	},
	//安卓隐藏底部tab栏
	hideBottomTab:function(){
		this._process(null,"NativeBottomBarPlugin","hideBottomBar");
	},
	//安卓显示底部tab栏
	showBottomTab:function(){
		this._process(null,"NativeBottomBarPlugin","showBottomBar");
	},

};

module.exports=ServiceApi;